//
//  SSCShareUnityAdapter.m
//  Unity-iPhone
//
//  Created by xuzepei on 16/1/19.
//
//

#import "SSCShareUnityAdapter.h"
#if __has_include(<SSCAdsSDK/SSCAdsSDK.h>)
#import <SSCAdsSDK/SSCAdsSDK.h>
#else
#import "AdIdHelper.h"
#endif

@implementation SSCShareUnityAdapter

+ (SSCShareUnityAdapter*)getInstance
{
    static SSCShareUnityAdapter* sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token, ^{
        sharedInstance = [[SSCShareUnityAdapter alloc] init];
        [SSCShareController getInstance].delegate = sharedInstance;
    });
    
    return sharedInstance;
}

- (void)setRootViewController:(UIViewController*)vc
{
    [SSCShareController getInstance].rootViewController = vc;
}

- (void)setGameObjectName:(char*)name
{
    if(name && strlen(name))
    {
        self.objectName = [NSString stringWithFormat:@"%s",name];
    }
}

- (void)shareToAll:(char*)message imagePath:(char *)imagePath
{
    NSString* para0 = nil;
    NSString* para1 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    if(imagePath && strlen(imagePath))
        para1 = [NSString stringWithFormat:@"%s",imagePath];
    
    [[SSCShareController getInstance] shareToAll:para0 imagePath:para1];
}

- (void)shareToAll:(char*)message image:image
{
    NSString* para0 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    [[SSCShareController getInstance] shareToAll:para0 image:image];
}

- (void)shareToEmail:(char*)subject message:(char*)message imagePath:(char*)imagePath
{
    NSString* para0 = nil;
    NSString* para1 = nil;
    NSString* para2 = nil;
    
    if(subject && strlen(subject))
        para0 = [NSString stringWithFormat:@"%s",subject];
    
    if(message && strlen(message))
        para1 = [NSString stringWithFormat:@"%s",message];
    
    if(imagePath && strlen(imagePath))
        para2 = [NSString stringWithFormat:@"%s",imagePath];
    
    [[SSCShareController getInstance] shareToEmail:para0 message:para1 imagePath:para2];
}

- (void)shareToEmail:(char*)subject message:(char*)message image:(UIImage*)image
{
    NSString* para0 = nil;
    NSString* para1 = nil;
    
    if(subject && strlen(subject))
        para0 = [NSString stringWithFormat:@"%s",subject];
    
    if(message && strlen(message))
        para1 = [NSString stringWithFormat:@"%s",message];
    
    [[SSCShareController getInstance] shareToEmail:para0 message:para1 image:image];
}

- (void)shareToFacebook:(char*)message imagePath:(char*)imagePath
{
    NSString* para0 = nil;
    NSString* para1 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    if(imagePath && strlen(imagePath))
        para1 = [NSString stringWithFormat:@"%s",imagePath];
    
    [[SSCShareController getInstance] shareToFacebook:para0 imagePath:para1];
}

- (void)shareToFacebook:(char*)message image:image
{
    NSString* para0 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    [[SSCShareController getInstance] shareToFacebook:para0 image:image];
}

- (void)shareToTwitter:(char*)message imagePath:(char*)imagePath
{
    NSString* para0 = nil;
    NSString* para1 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    if(imagePath && strlen(imagePath))
        para1 = [NSString stringWithFormat:@"%s",imagePath];
    
    [[SSCShareController getInstance] shareToTwitter:para0 imagePath:para1];
}

- (void)shareToTwitter:(char*)message image:image
{
    NSString* para0 = nil;
    
    if(message && strlen(message))
        para0 = [NSString stringWithFormat:@"%s",message];
    
    [[SSCShareController getInstance] shareToTwitter:para0 image:image];
}

- (void)saveToPhotoLibrary:(UIImage*)image
{
    if(image)
        [[SSCShareController getInstance] saveToPhotoLibrary:image];
}

- (void)openUrl:(char*)url
{
    NSString* para1 = nil;
    
    if(url && strlen(url))
        para1 = [NSString stringWithFormat:@"%s",url];
    
    if([para1 length])
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:para1]];
    }
}

#pragma mark - SSCShareControllerDelegate

- (const char*)getJsonString:(NSDictionary*)dictonary
{
    NSString *jsonString = nil;
    if(dictonary)
    {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictonary
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];
        
        if (! jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }
    
    if([jsonString length])
    {
        return [jsonString UTF8String];
    }
    
    return "";
}

- (void)shareSuccessed:(ShareType)type
{
    static NSString *methodName = @"OnShareSuccessed";
    if([self.objectName length])
    {
        NSDictionary* dict = @{@"share_type":[NSString stringWithFormat:@"%d",(int)type]};
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],[self getJsonString:dict]);
    }
}

- (void)shareFailed:(ShareType)type errorMessage:(NSString*)errorMessage
{
    static NSString *methodName = @"OnShareFailed";
    if([self.objectName length])
    {
        if(0 == [errorMessage length])
            errorMessage = @"";
        
        NSDictionary* dict = @{@"share_type":[NSString stringWithFormat:@"%d",(int)type],@"error_message":errorMessage};
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],[self getJsonString:dict]);
    }
}

- (void)saveImageSuccessed
{
    static NSString *methodName = @"OnSaveImageSuccessed";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

- (void)saveImageFailed:(NSString*)errorMessage
{
    static NSString *methodName = @"OnSaveImageFailed";
    if([self.objectName length])
    {
        if(0 == [errorMessage length])
            errorMessage = @"";
        
        NSDictionary* dict = @{@"error_message":errorMessage};
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],[self getJsonString:dict]);
    }
}

@end

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
    
    //To set the name of game object for UnitySendMessage method
    void _SetGameObjectNameForShare(char* gameObjectName)
    {
        [[SSCShareUnityAdapter getInstance] setGameObjectName:gameObjectName];
    }
    
    void _ShareToAll(char* message, char* imagePath)
    {
        [[SSCShareUnityAdapter getInstance] shareToAll:message imagePath:imagePath];
    }
    
    void _ShareToEmail(char*subject, char* message, char* imagePath)
    {
        [[SSCShareUnityAdapter getInstance] shareToEmail:subject message:message imagePath:imagePath];
    }
    
    void _ShareToFacebook(char* message, char* imagePath)
    {
        [[SSCShareUnityAdapter getInstance] shareToFacebook:message imagePath:imagePath];
    }
    
    void _ShareToTwitter(char* message, char* imagePath)
    {
//        UIImage *image = nil;
//        NSData *data = [NSData dataWithBytes:imageBytes length:length];
//        if(data)
//        {
//            image = [UIImage imageWithData:data];
//        }
        
        [[SSCShareUnityAdapter getInstance] shareToTwitter:message imagePath:imagePath];
    }
    
    void _SaveImage(char* path, const Byte* imageBytes, int length)
    {
        NSData *data = [NSData dataWithBytes:imageBytes length:length];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            if(image)
                [[SSCShareUnityAdapter getInstance] saveToPhotoLibrary:image];
        }
    }
    
//    void _OpenUrl(char* url)
//    {
//        [[SSCShareUnityAdapter getInstance] openUrl:url];
//    }
}
